package com.zjuee.web.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjuee.base.bean.response.CommonResultCode;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.enums.CommonNumber;
import com.zjuee.base.enums.CommonString;
import com.zjuee.base.enums.RedisCacheType;
import com.zjuee.base.exception.BaseException;
import com.zjuee.base.utils.DateUtils;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.core.translation.utils.CaseUtil;
import com.zjuee.service.company.service.CompanyLicenseClassService;
import com.zjuee.service.company.service.CompanyLicenseService;
import com.zjuee.service.company.vo.CompanyLicenseVo;
import com.zjuee.base.model.common.company.CompanyLicense;
import com.zjuee.base.model.common.company.CompanyLicenseClass;
import com.zjuee.web.company.mapper.CompanyLicenseClassMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CompanyLicenseClassServiceImpl extends ServiceImpl<CompanyLicenseClassMapper, CompanyLicenseClass> implements CompanyLicenseClassService {

    @Resource
    private CompanyLicenseClassMapper companyLicenseClassMapper;

    @Resource
    private CompanyLicenseService licenseService;

    @Resource
    private LoginDto loginDto;

    @Resource
    private TranslateData translateData;

    @Override
    public List<CompanyLicenseClass> getAllBClass(Long companyId, String licenseType) {
        LambdaQueryWrapper<CompanyLicense> licenseWrapper = Wrappers.<CompanyLicense>query().lambda();
        licenseWrapper.eq(CompanyLicense::getBciId, loginDto.getOrgBusinessId())
                      .eq(CompanyLicense::getBclStatus, 1)
                      .le(CompanyLicense::getBclValidStartDate, DateUtils.parse(new Date(), "yyyyMMdd"))
                      .ge(CompanyLicense::getBclValidEndDate, DateUtils.parse(new Date(), "yyyyMMdd"));

        licenseWrapper.select(CompanyLicense::getId);
        if (StringUtils.equalsIgnoreCase("12", licenseType)){
            licenseWrapper.eq(CompanyLicense::getBclMedical , CommonNumber.YES.getValue());
        }
        //查询该企业的许可证状态
        List<CompanyLicense>  licenseList = licenseService.list(licenseWrapper);
        if(licenseList.isEmpty()){
            throw new BaseException(CommonResultCode.CONSUMER_ERR, "无有效企业许可证");
        }
        //转换为Set<Long>类型
        Set<Long> longSet = (Set<Long>) licenseList.stream().map(x -> x.getId()).collect(Collectors.toSet());
        LambdaQueryWrapper<CompanyLicenseClass> queryWrapper = Wrappers.<CompanyLicenseClass>query().lambda();
        if (StringUtils.equalsIgnoreCase("12", licenseType)){
            queryWrapper.eq(CompanyLicenseClass::getBclcBcode , CommonString.MED_WASTER.getValue());
        }
        queryWrapper.in(CompanyLicenseClass::getBclId, longSet);
        queryWrapper.groupBy(CompanyLicenseClass::getBclcBcode);
        queryWrapper.select(CompanyLicenseClass::getBclcBcode, CompanyLicenseClass::getBclId);
        return this.list(queryWrapper);
    }

    @Override
    public List<CompanyLicenseClass> getAllSClass(Long licenseId, String bCode) {
        LambdaQueryWrapper<CompanyLicenseClass> queryWrapper = Wrappers.<CompanyLicenseClass>query().lambda();
        queryWrapper.eq(CompanyLicenseClass::getBclcBcode, bCode);
        queryWrapper.eq(CompanyLicenseClass::getBclId, licenseId);
        queryWrapper.groupBy(CompanyLicenseClass::getBclcScode);
        queryWrapper.select(CompanyLicenseClass::getBclcScode, CompanyLicenseClass::getBclId);
        return this.list(queryWrapper);



    }

    @Override
    public List<CompanyLicenseVo> getAllBDisposal(Long licenseId, String sCode) {
        List<CompanyLicenseVo> companyLicenseVoList = companyLicenseClassMapper.
                getAllBDisposal(licenseId, sCode);
        List<CompanyLicenseVo> licenseVoList = (List<CompanyLicenseVo>) CaseUtil
                .castList(translateData
                        .translateData(companyLicenseVoList, "companyLicenseListHandle",
                                String.valueOf(RedisCacheType.DISPOSE_TYPE)), CompanyLicenseVo.class);;
        //翻译数据
        return licenseVoList;
    }

    @Override
    public List<CompanyLicenseVo> getAllLicense(String bCode, String sCode) {
        return companyLicenseClassMapper.getAllLicense(bCode, sCode);
    }

}
